package com.hy.mydesktop.client.mvc.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Component;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.GxtComponentMetaModel;
import com.hy.mydesktop.client.component.meta.GxtComponentMetaNodeModel;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.component.mvc.factory.GxtComponentControlerFactory;
import com.hy.mydesktop.client.component.type.ComponentControllerEnum;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.util.ControllerUtil;

public class FormSetModuleControler extends BaseController {

	private FormSetModuleView formSetModulesView;
	private FormModuleView formModelView;

	public FormSetModuleControler() {
		// registerEventTypes(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
		
	}

	@Override
	public void handleEvent(AppEvent event) {
		Map<String, Object> map = (Map<String, Object>) event.getData();
		EventStructureMetaModel eventStructureMetaModel = (EventStructureMetaModel) map
				.get("eventstructuremetamodel");
		// EventStructureMetaModel eventStructureMetaModel
		// =(EventStructureMetaModel)((Map<String,
		// Object>)event.getData()).get("eventstructuremetamodel");
		// 0007
		if (AppEvents.DESKTOP_WINDOWAPPLICATION_INIT == event.getType()) {
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				// 转发到视图View
				forwardToView(formSetModulesView, event);

				/**
				 * 获取Controller对应的View的组件Component 把Component，作为事件的一部分，向下传递
				 * map.put("parent", windowView.getWindow()); 此方法错误
				 */
				Map<BaseController, Component> subMap = (Map<BaseController, Component>) map
						.get("controllerandcomponent");
				;
				subMap.put(this, formSetModulesView.getFormPanel());
				/**
				 * 把Controller和,与之对应的Component放入Map（为了方便查找Component的父元素）
				 */
				map.put("controllerandcomponent", subMap);
				/**
				 * 如何知道事件已经转发完毕
				 * 
				 * 方法1 ： 计数器
				 */
				/**
				 * 1判断是否应该子Controller
				 * map.get("formSetModuleChildren")或者通过Controller.getControllerData()
				 * 2读取出所有子Controller的标识， 
				 * List   
				 * 3把这些标识作为参数，调用后台业务逻辑方法
				 * List<GxtComponentMetaNodeModel>
				 * 4根据返回的结果，创建子Controller
				 * 5然后逐一加入到当前Controller
				 */
				
				/**
				 * 由于Controller具有唯一的uuid，所以当Controller的uuid确定以后，
				 * 就已经确立了Controller的树形结构
				 * 可以判断当前的Controller是否有子节点Controller
				 */
				//是有有孩子，有添加进去
				String controllerData = this.getControllerData();
				if (controllerData!=null) {
					//假设调用后台业务逻辑，获得List<GxtComponentMetaNodeModel>
					List<GxtComponentMetaNodeModel> list = this.getGxtComponentMetaNodeModelList();
					for (int i = 0; i < list.size(); i++) {
						Controller child = GxtComponentControlerFactory.createOneGxtComponentController(list.get(i));
						this.addChild(child);
					}
					
				}
				/*if (((Integer) map.get("sub")) > 0) {
					for (int i = 0; i < ((Integer) map.get("sub")); i++) {
						Map<String, Object> formPanelController = new HashMap<String, Object>();
						formPanelController
								.put("component_controller_model_type",
										ComponentControllerEnum.FORMMODULE_CONTROLLER_MODEL);
						List<String> codes5 = new ArrayList<String>();
						codes5.add("0201");
						codes5.add("0101");
						codes5.add("0007");
						codes5.add("0008");
						formPanelController.put("registereventtypes", codes5);
						formPanelController.put("id", "formPanelController");
						formPanelController.put("viewdata",
								"formPanel_viewData_init_01");
						GxtComponentMetaNodeModel node5 = new GxtComponentMetaNodeModel(
								formPanelController, 1, 2);
						List<GxtComponentMetaNodeModel> list = new ArrayList<GxtComponentMetaNodeModel>();
						list.add(node5);
						GxtComponentMetaModel gxtComponentMetaModel = new GxtComponentMetaModel(
								list);
						Controller chidle = GxtComponentControlerFactory
								.createGxtComponentController(gxtComponentMetaModel);
						this.addChild(chidle);
					}
					map.put("sub", 0);
				}*/

				if (this.getChildren() != null && this.getChildren().size() > 0) {
					/**
					 * 将事件转发给子节点，及子控制器 注:当前控制器，含有子节点的时候，必不可少
					 * 
					 * 采用先序遍历的方式---树
					 */
					this.forwardToChild(new AppEvent(
							AppEvents.DESKTOP_WINDOWAPPLICATION_INIT, map));
				} /*
				 * else {
				 */
				/**
				 * 如果当前控制器，有子节点，则向下转发事件， 否则渲染组件，即渲染顶级元素 如何渲染顶级元素，即Window ？
				 */
				/*
				 * forwardToView(windowView,
				 * AppEvents.DESKTOP_APPLICATION_RENDER,event.getData()); }
				 */
				/*int count = ((Integer) map.get("count") - 1);
				if (count <= 0) {
					Dispatcher.forwardEvent(
							AppEvents.DESKTOP_APPLICATION_RENDER, map);
				} else {
					map.put("count", count);
				}*/
				ControllerUtil.isRenderer(map);
			}
		}else {
			this.forwardToChild(event);
		}
		;
	}

	public void initialize() {
		formSetModulesView = new FormSetModuleView(this);
	}

	protected void onError(AppEvent ae) {
		System.out.println("error: " + ae.<Object> getData());
	}
	
	private List<GxtComponentMetaNodeModel> getGxtComponentMetaNodeModelList() {
		
		Map<String, Object> formPanelController = new HashMap<String, Object>();
		formPanelController
				.put("component_controller_model_type",
						ComponentControllerEnum.FORMMODULE_CONTROLLER_MODEL);
		List<String> codes5 = new ArrayList<String>();
		codes5.add("0201");
		codes5.add("0101");
		codes5.add("0007");
		codes5.add("0008");
		formPanelController.put("registereventtypes", codes5);
		formPanelController.put("id", "formPanelController");
		formPanelController.put("viewdata",
				"formPanel_viewData_init_01");
		GxtComponentMetaNodeModel node5 = new GxtComponentMetaNodeModel(
				formPanelController, 1, 2);
		List<GxtComponentMetaNodeModel> list = new ArrayList<GxtComponentMetaNodeModel>();
		list.add(node5);
		//list.add(node5);
		//GxtComponentMetaModel gxtComponentMetaModel = new GxtComponentMetaModel(list);
		return list;
	}
}
